package com.mdd.maduoduo.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mdd.maduoduo.R;
import com.mdd.maduoduo.activity.MaxActivity;
import com.mdd.maduoduo.activity.SelectTimeActivity;
import com.mdd.maduoduo.activity.SettingActivity;
import com.mdd.maduoduo.activity.ShareCodeActivity;
import com.mdd.maduoduo.adapter.BottomDataAdapter;
import com.mdd.maduoduo.di.component.DaggerUIComponent;
import com.mdd.maduoduo.di.component.UIComponent;
import com.mdd.maduoduo.entity.MaxConfigEntity;
import com.mdd.maduoduo.entity.UserEntity;
import com.mdd.maduoduo.presenter.LoginPresenter;
import com.mdd.maduoduo.utils.AlertUtils;
import com.mdd.maduoduo.utils.CommonAdapter;
import com.mdd.maduoduo.utils.GlideUtils;
import com.mdd.maduoduo.utils.ToastUtils;
import com.mdd.maduoduo.utils.Utils;
import com.mdd.maduoduo.view.interfaces.GetUserView;
import com.mdd.maduoduo.view.interfaces.MaxConfigView;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.config.IndicatorConfig;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

public class MineFragment extends BaseFragment implements GetUserView, MaxConfigView {

    @Inject
    LoginPresenter mPresenter;

    private TextView tvNickName,tvSologo;

    private ImageView ivIcon;

    private ImageView ivLabel;

    private Banner banner;

    private RecyclerView recyclerView;

    private Switch switchOne,switchTwo;

    //判断用户是否是会员
    private int isMax;

    private TextView tvAdd;

    private TextView tvTitle;

    private TextView tvTime;

    private LinearLayout llSettingOne,llSettingTwo;

    private TextView tvMangerNum;
    private TextView tvNoLimit;

    private ImageView ivSetting;

    private ImageView ivQuestionOne,ivQuestionTwo;

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initializeInjector() {
        super.initializeInjector();
        mPresenter = new LoginPresenter();
        UIComponent uiComponent = DaggerUIComponent.builder().applicationComponent(getApplicationComponent())
                .activityModule(getActivityModule()).build();
        uiComponent.inject(this);
        mPresenter.setGetUserView(this);
        mPresenter.setMaxConfigView(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getUser();
        mPresenter.getMaxConfig();
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        ivIcon = view.findViewById(R.id.ivIcon);
        tvNickName = view.findViewById(R.id.tvNickName);
        tvSologo = view.findViewById(R.id.tvSologo);
        ivLabel = view.findViewById(R.id.ivLabel);
        banner = view.findViewById(R.id.banner);
        recyclerView = view.findViewById(R.id.recyclerView);
        switchOne = view.findViewById(R.id.switchOne);
        switchTwo = view.findViewById(R.id.switchTwo);
        tvAdd = view.findViewById(R.id.tvAdd);
        tvTitle = view.findViewById(R.id.tvTitle);
        tvTime = view.findViewById(R.id.tvTime);
        llSettingOne = view.findViewById(R.id.llSettingOne);
        llSettingTwo = view.findViewById(R.id.llSettingTwo);
        tvMangerNum = view.findViewById(R.id.tvMangerNum);
        tvNoLimit = view.findViewById(R.id.tvNoLimit);
        ivSetting = view.findViewById(R.id.ivSetting);
        ivQuestionOne = view.findViewById(R.id.ivQuestionOne);
        ivQuestionTwo = view.findViewById(R.id.ivQuestionTwo);
    }

    @Override
    protected void onClick() {
        ivQuestionOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertUtils.tipsAlert(getActivity());
            }
        });

        ivQuestionTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertUtils.tipsAlert(getActivity());
            }
        });
    }

    @Override
    public void renderSuccess(UserEntity userEntity) {
        GlideUtils.getInstances().loadNormalImg(getActivity(),ivIcon,userEntity.data.personalFrontPhoto);
        tvNickName.setText(userEntity.data.nickName);
        tvSologo.setText(userEntity.data.sologo);

        ivSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isFastClick()){
                    Intent intent = new Intent(getActivity(), SettingActivity.class);
                    intent.putExtra("nick",userEntity.data.nickName);
                    intent.putExtra("id",userEntity.data.id);
                    intent.putExtra("pickupSharedCode",userEntity.data.pickupSharedCode);
                    getActivity().startActivity(intent);
                }
            }
        });

        //isMax 0-不是 1-是
        if (userEntity.data.isMax==0){
            ivLabel.setVisibility(View.INVISIBLE);
        }else {
            ivLabel.setVisibility(View.VISIBLE);
        }
        //动态功能模块
        List<UserEntity.DataDTO.FunctionConfigDtosDTO>list = userEntity.data.functionConfigDtos;

        List<UserEntity.DataDTO.FunctionConfigDtosDTO>listBanner  = new ArrayList<>();
        List<UserEntity.DataDTO.FunctionConfigDtosDTO>listItem  = new ArrayList<>();
        //遍历拆分轮播图与 选项
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).module == 0){
                listBanner.add(list.get(i));
            }else {
                listItem.add(list.get(i));
            }
        }
        if (!listBanner.get(0).itemDtos.isEmpty()){
            banner.setVisibility(View.VISIBLE);
            //加载banner数据
            banner.setAdapter(new BannerImageAdapter<UserEntity.DataDTO.FunctionConfigDtosDTO.ItemDtosDTO>(listBanner.get(0).itemDtos) {
                @Override
                public void onBindView(BannerImageHolder holder, UserEntity.DataDTO.FunctionConfigDtosDTO.ItemDtosDTO data, int position, int size) {
                    GlideUtils.getInstances().loadNormalImg(getActivity(),holder.imageView,data.url);
                }
            });
            banner.setOnBannerListener(new OnBannerListener() {
                @Override
                public void OnBannerClick(Object data, int position) {
                    //根据jumpType判断跳转位置 点击跳转类型:0-跳转网页、1-跳转五星好评、2-执行分享功能、3-跳转打赏界面、4-跳转Max会员购买界面、6-跳转小程序、7-客服电话(有拨打效果)、8-仅展示(点击可复制)、9-跳转多人共享界面
                    UserEntity.DataDTO.FunctionConfigDtosDTO.ItemDtosDTO bean = (UserEntity.DataDTO.FunctionConfigDtosDTO.ItemDtosDTO) data;
                    Utils.jump(getActivity(),bean);
                }
            });
            // 设置指示器
            banner.setIndicator(new CircleIndicator(this.getContext()));
            // 设置指示器位置
            banner.setIndicatorGravity(IndicatorConfig.Direction.CENTER);
            banner.start();
        }else {
            banner.setVisibility(View.GONE);
        }

        tvTitle.setText(listItem.get(0).moduleName);
        //设置底部数据
        BottomDataAdapter bottomDataAdapter = new BottomDataAdapter();
        recyclerView.setAdapter(bottomDataAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        bottomDataAdapter.setData(listItem.get(0).itemDtos);

        bottomDataAdapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener<UserEntity.DataDTO.FunctionConfigDtosDTO.ItemDtosDTO>() {
            @Override
            public void onItemClick(CommonAdapter<UserEntity.DataDTO.FunctionConfigDtosDTO.ItemDtosDTO> adapter, View view, int position, UserEntity.DataDTO.FunctionConfigDtosDTO.ItemDtosDTO item) {
                Utils.jump(getActivity(),item);
            }
        });
    }

    @Override
    public void renderSuccess(MaxConfigEntity maxConfigEntity) {
        isMax = maxConfigEntity.data.isMax;
        tvMangerNum.setText(maxConfigEntity.data.managedCodeName);
        //先赋值选中状态
        if (maxConfigEntity.data.shareCodeSwitchStatus != 0){
            switchOne.setChecked(true);
        }else {
            switchOne.setChecked(false);
        }

        if (maxConfigEntity.data.scheduleTimeSwitchStatus != 0){
            switchTwo.setChecked(true);
        }else {
            switchTwo.setChecked(false);
        }
        if (isMax==1){
            switchOne.setEnabled(true);
            switchTwo.setEnabled(true);

            switchOne.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(@NonNull CompoundButton compoundButton, boolean b) {
                    if (b){
                        mPresenter.setSwitch(1,1);
                    }else {
                        mPresenter.setSwitch(1,0);
                    }
                }
            });

            switchTwo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(@NonNull CompoundButton compoundButton, boolean b) {
                    if (b){
                        mPresenter.setSwitch(2,1);
                    }else {
                        mPresenter.setSwitch(2,0);
                    }
                }
            });

        }else {
            //拦截转换时间并且跳转会员页面
            switchOne.setEnabled(true);
            switchTwo.setEnabled(true);

            /*switchOne.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 立即重置状态
                    ToastUtils.showLongToast(getActivity(),"当前功能为Max会员功能,请先开通Max会员");
                    startActivity(new Intent(getActivity(), MaxActivity.class));
                }
            });
            switchTwo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 立即重置状态
                    ToastUtils.showLongToast(getActivity(),"当前功能为Max会员功能,请先开通Max会员");
                    startActivity(new Intent(getActivity(), MaxActivity.class));
                }
            });*/

            switchOne.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(@NonNull CompoundButton compoundButton, boolean b) {
                    if(isMax==0){
                        // 立即重置状态
                        switchOne.setChecked(!b);
                        ToastUtils.showLongToast(getActivity(),"当前功能为Max会员功能,请先开通Max会员");
                        startActivity(new Intent(getActivity(), MaxActivity.class));
                    }else {
                        if (b){
                            mPresenter.setSwitch(1,1);
                        }else {
                            mPresenter.setSwitch(1,0);
                        }
                    }
                }
            });
            switchTwo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(@NonNull CompoundButton compoundButton, boolean b) {
                    if(isMax==0){
                        // 立即重置状态
                        switchTwo.setChecked(!b);
                        ToastUtils.showLongToast(getActivity(),"当前功能为Max会员功能,请先开通Max会员");
                        startActivity(new Intent(getActivity(), MaxActivity.class));
                    }else {
                        if (b){
                            mPresenter.setSwitch(2,1);
                        }else {
                            mPresenter.setSwitch(2,0);
                        }
                    }
                }
            });
        }

        if (isMax == 1) {
            tvAdd.setVisibility(View.VISIBLE);
        }

        if (isMax==1&&!maxConfigEntity.data.scheduleTime.isEmpty()){
            tvTime.setText(maxConfigEntity.data.scheduleTime);
        }

        llSettingOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*if (isMax==1){
                    //是会员跳转页面
                    startActivity(new Intent(getActivity(), ShareCodeActivity.class));
                }else {
                    //不是会员跳转购买会员页面
                    ToastUtils.showLongToast(getActivity(),"当前功能为Max会员功能,请先开通Max会员");
                    startActivity(new Intent(getActivity(), MaxActivity.class));
                }*/
                startActivity(new Intent(getActivity(), ShareCodeActivity.class));
            }
        });

        llSettingTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*if (isMax==1){
                    //是会员跳转页面
                    Intent intent = new Intent(getActivity(), SelectTimeActivity.class);
                    intent.putExtra("scheduleTime",maxConfigEntity.data.scheduleTime);
                    startActivity(intent);
                }else {
                    //不是会员跳转购买会员页面
                    ToastUtils.showLongToast(getActivity(),"当前功能为Max会员功能,请先开通Max会员");
                    startActivity(new Intent(getActivity(), MaxActivity.class));
                }*/
                Intent intent = new Intent(getActivity(), SelectTimeActivity.class);
                intent.putExtra("scheduleTime",maxConfigEntity.data.scheduleTime);
                startActivity(intent);
            }
        });

        tvMangerNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isMax==1){
                    ToastUtils.showLongToast(getActivity(),"当前已是不限制状态");
                }else {
                    ToastUtils.showLongToast(getActivity(),"当前功能为Max会员功能,请先开通Max会员");
                    startActivity(new Intent(getActivity(), MaxActivity.class));
                }
            }
        });
        tvNoLimit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isMax==1){
                    ToastUtils.showLongToast(getActivity(),"当前已是不限制状态");
                }else {
                    ToastUtils.showLongToast(getActivity(),"当前功能为Max会员功能,请先开通Max会员");
                    startActivity(new Intent(getActivity(), MaxActivity.class));
                }
            }
        });



    }
}
